package com.example.accessibilityservice;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.accessibilityservice.GestureDescription;
import android.app.Notification;
import android.app.PendingIntent;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.example.accessibilityservice.process.AccessibilityProcessBase;
import com.example.accessibilityservice.process.WXAccessibilityProcess;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import androidx.annotation.NonNull;

public class MyAccessibilityService extends AccessibilityService implements AccessibilityServiceInterface{
    private static final String TAG = MyAccessibilityService.class.getSimpleName();
    private List<AccessibilityProcessBase> accessibilityProcessBaseList;
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(TAG, "onServiceConnected.....");
        accessibilityProcessBaseList = new ArrayList<>();
        accessibilityProcessBaseList.add(new WXAccessibilityProcess(this));
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        if(null != accessibilityEvent){
            String packageName = accessibilityEvent.getPackageName().toString();
            /// 微信包名
            if(!TextUtils.isEmpty(packageName) && !accessibilityProcessBaseList.isEmpty()){
                for(AccessibilityProcessBase process:accessibilityProcessBaseList){
                    if(process.getPackageName().equals(packageName.trim())){
                        process.processEvent(accessibilityEvent,getRoot());
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void onInterrupt() {

    }

    @Override
    public void performBackBtn(){
        if(performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)){
            Log.w(TAG, "返回成功。");
        } else {
            Log.w(TAG, "返回失败。");
        }
    }

    @Override
    public void performMove(Path path, GestureResultCallback callback){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            dispatchGesture(new GestureDescription.Builder()
                    .addStroke(new GestureDescription.StrokeDescription(path,0,500))
                    .build(),callback,null);
        }
    }

    @Override
    public AccessibilityNodeInfo getRoot() {
        return getRootInActiveWindow();
    }
}
